package com.hoasua.adb.example;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.hoasua.adb.adbManager;

/**
 * this class extends aDbManager: abstract class
 * */
public class ObjectManagerExample extends adbManager{
	
	public static final String TAG="Class: UserManager";
	
	Context context;

	public static final String TABLE = "tbl_user";

	public static final String _id_username = "id_username";
	public static final String _fullname = "fullname";
	public static final String _gender = "gender";
	public static final String _birthday = "birthday";
	public static final String _email = "email";
	public static final String _link_image = "link_image";
	public static final String _type_account = "type_account";
	public static final String _address = "address";
	public static final String _latitude = "latitude";
	public static final String _longitude = "longitude";
	public static final String _current_address = "current_address";
	public static final String _phone = "phone";
	public static final String _nickname = "nickname";
	public static final String _category = "category";

	public static final String[] COLUMNS = { adbManager._id, _id_username,
			_fullname, _gender, _birthday, _email, _link_image, _type_account,
			_address, _latitude, _longitude, _current_address, _phone,
			_nickname, _category };
	
	public ObjectManagerExample(Context context) {
		super(TABLE,COLUMNS);
		help=new DataBaseHelper(context);//IMPORTANT!! must be called before init()
		super.init(help);	//IMPORTANT!!
	}
	@Override
	public ArrayList<ObjectExample> select() {
		ArrayList<ObjectExample> array = new ArrayList<ObjectExample>();
		SQLiteDatabase db = help.getReadableDatabase();
		Cursor cursor = db.query(TABLE, COLUMNS, null, null, null, null,
				adbManager._id);
		array=getArrayObjectFromCursor(cursor);
		db.close();
		help.close();
		return array;
	}

	@Override
	public ObjectExample select(int id) {
		ObjectExample object = null;
		if (id >= 0) {
			SQLiteDatabase db = help.getReadableDatabase();
			Cursor cursor = db.query(TABLE, COLUMNS, adbManager._id + "='" + id
					+ "'", null, null, null, adbManager._id);
			cursor.moveToFirst();
			
			object=getObjectFromCusor(cursor);
			db.close();
			help.close();
		}
		return object;
	}

	@Override
	public ArrayList<ObjectExample> select(String field,String value){
		ArrayList<ObjectExample> array = new ArrayList<ObjectExample>();
		SQLiteDatabase db = help.getReadableDatabase();
		for(String tmp:COLUMNS)
			if(tmp.equals(field)){
				break;
			}else{
				Log.e(TAG, "not matching field to SELECT");
				return array;
			}
		Cursor cursor = db.query(TABLE, COLUMNS, field + "='" + value
				+ "'", null, null, null, adbManager._id);
		
		array=getArrayObjectFromCursor(cursor);
		db.close();
		help.close();
		return array;
	}
	
	@Override
	public ObjectExample getObjectFromCusor(Cursor cursor){
		ObjectExample t=null;
		cursor.moveToFirst();
		
		if(cursor.getCount()==0){
			Log.e(TAG, "cursor is null");
			cursor.close();
			return t;
		}
		do {
			int _id = cursor.getInt(cursor.getColumnIndex(adbManager._id));
			int size = COLUMNS.length-1;
			String[] values = new String[size];
			for (int i = 0; i < size; ++i) {
				values[i] = cursor.getString(i + 1);
			}
			t = new ObjectExample(_id, values);
		} while (cursor.moveToNext());
		cursor.close();
		return t;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<ObjectExample> getArrayObjectFromCursor(Cursor cursor){
		ArrayList<ObjectExample> array = new ArrayList<ObjectExample>();
		if(cursor.getCount()==0){
			Log.e(TAG, "cursor is null");
			cursor.close();
			return array;
		}
		
		cursor.moveToFirst();
		do {
			int _id = cursor.getInt(cursor.getColumnIndex(adbManager._id));
			int size = COLUMNS.length-1;
			String[] values = new String[size];
			for (int i = 0; i < size; ++i) {
				values[i] = cursor.getString(i + 1);
			}
			ObjectExample t = new ObjectExample(_id, values);
			array.add(t);
		} while (cursor.moveToNext());
		cursor.close();
		return array;
	}
}
